package cn.mengfly.product.hysitrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

/**
 * 快速失败策略
 *
 * @author Mengfly
 * @date 2021/3/13 16:13
 */
public class CommandThatFailFast extends HystrixCommand<String> {
    /**
     * 用来标识是否需要抛出异常
     */
    private final boolean throwException;

    protected CommandThatFailFast(boolean throwException) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.throwException = throwException;
    }

    @Override
    protected String run() throws Exception {
        if (throwException) {
            throw new RuntimeException("failure from CommandThatFails Fast");
        } else {

            return "SUCCESS";
        }
    }

    ////////////// 静默失败即当进行服务降级处理时返回空的结果，针对返回值类型，返回的可能是null、空List或者空Map等。

    @Override
    protected String getFallback() {
        return null;
    }
}
